/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package easybooking.clients.gui.report;

import com.toedter.calendar.JDateChooser;
import easybooking.ReportClientController;
import java.awt.Component;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.UIManager;

/**
 *
 * @author Tim Schröder
 * http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#sorting
 * http://stackoverflow.com/questions/1260112/combining-java-util-dates-to-create-a-date-time
 * http://jdatechooser.sourceforge.net/
 */
public class RCCustomerReportDialogDateSelect extends JDialog {

    private ReportClientController rcc;
    private JPanel buttonPanel;
    private JButton btnSelectDate;
    private JButton btnCancel;
    private JDateChooser dateChooser;
    private JPanel dialogPanel;
    private String buttonName;
    private Date date = null;

    /**
     * Constructor of the RCCustomerReportDialogDateSelect class, which sets
     * the controller
     * @param rcif the ReportClientController
     */
    public RCCustomerReportDialogDateSelect(ReportClientController rcif) {
        rcc = rcif;
    }

    /**
     * get the selected date
     * @return the selected date
     */
    public Date getDate() {
        date = dateChooser.getDate();
        // get die spinners editor
        // JSpinner.DateEditor edit = (JSpinner.DateEditor) timeSpinner.getEditor();
        // get the time String
        //String timeString = edit.getTextField().getText();
        // define time format
        SimpleDateFormat df = new SimpleDateFormat("HH:mm");
        Date time = null;
        return this.date;
    }

    /**
     * sets the Date for a flight
     * @param d is the date which is selected
     */
    public void setDate(Date d) {
        this.date = d;
    }

    /**
     * sets the button name
     * @param buttonName
     */
    public void setButtonName(String buttonName) {
        this.buttonName = buttonName;
    }

    /**
     * calls the generateComponents() method to create and show the FlightDialogDateSelect
     */
    public void createAndShowDialog() {
        generateComponents();
        this.setResizable(true);
        this.setTitle("Select Departure");
        this.setModal(true); // deaktivates input to other dialogs
        this.setLocationRelativeTo(null);
        this.pack();
        this.setVisible(true);
        this.toFront();

    }

    /**
     * Generates all components to create and display the GUI
     */
    private void generateComponents() {
        dialogPanel = new JPanel();

        // sets system default look and feel
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception exc) {
            System.err.println("Error loading L&F: " + exc);
        }

        // Sets the border
        dialogPanel.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createTitledBorder("Select Date and Time "),
                BorderFactory.createEmptyBorder(20, 20, 20, 20)));

        dialogPanel.setLayout(new GridBagLayout());
        GridBagLayout gbl = new GridBagLayout();



        dateChooser = new JDateChooser();
        if (date != null) {
            dateChooser.setDate(date);
        }
        for (Component comp : dateChooser.getComponents()) {
            if (comp instanceof JTextField) {
                ((JTextField) comp).setColumns(50);
                ((JTextField) comp).setEditable(false);
            }
        }
        dialogPanel.add(dateChooser);

//        SpinnerModel model = new SpinnerDateModel();
//        timeSpinner = new JSpinner(model);
//        JComponent editor = new JSpinner.DateEditor(timeSpinner, "HH:mm");
//        timeSpinner.setEditor(editor);
//        if (date != null) {
//            timeSpinner.setValue(date);
//        }
//        dialogPanel.add(timeSpinner);

        // adds the buttton panel to the layout
        // Places the GUI elements in the layout using addComponent                                                 x, y, w, h, wx, wy
        addComponent(dialogPanel, gbl, this.createButtonPanel(), GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 0, 2, 3, 1, 0, 0);

        this.setContentPane(dialogPanel);

    }
    // Creates the button panel

    /**
     * Creates the buttons to select a departure or cancel
     * @return a JPanel with 2 buttons
     */
    private JPanel createButtonPanel() {

        buttonPanel = new JPanel();
        buttonPanel.setLayout(new GridBagLayout());
        GridBagLayout gbl = new GridBagLayout();

        btnSelectDate = new JButton("Select Date");
        btnSelectDate.setName(buttonName);
        btnSelectDate.setActionCommand("CustomerReportSelectDate");
        btnSelectDate.addActionListener(rcc);



        btnCancel = new JButton("Cancel");
        btnCancel.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });

        // Sets the buttons the same size
        btnCancel.setPreferredSize(btnSelectDate.getPreferredSize());

        // Adds the buttons to the buttonPanel
        addComponent(buttonPanel, gbl, btnSelectDate, GridBagConstraints.NONE, GridBagConstraints.WEST, 0, 0, 1, 1, 1, 0);
        addComponent(buttonPanel, gbl, btnCancel, GridBagConstraints.NONE, GridBagConstraints.EAST, 1, 0, 1, 1, 0, 0);

        return buttonPanel;
    }

    /**
     * defines the layout of the GUI
     * @param cont container to which the component is added
     * @param gbl GridBagLayout object
     * @param c compontent which is added to the container
     * @param fill how to use free space in the cell around the component
     * @param anchor where to fix an align the component
     * @param x x position in the grid layout
     * @param y y position in the grid layout
     * @param width number of columns the element should spread
     * @param hight number of rows the element should spread
     * @param weightx handels the horizontal free space
     * @param weighty handels the vertical free space
     */
    private static void addComponent(Container cont, GridBagLayout gbl, Component c, int fill, int anchor, int x, int y, int width, int hight, double weightx, double weighty) {
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = fill;
        gbc.anchor = anchor;
        gbc.gridx = x;
        gbc.gridy = y;
        gbc.gridwidth = width;
        gbc.gridheight = hight;
        gbc.weightx = weightx;
        gbc.weighty = weighty;
        gbc.insets = new Insets(10, 10, 10, 10);
        //gbl.setConstraints(c, gbc);
        cont.add(c, gbc);


    }
}
